package cn.walls1717.wallsmall.order.service.impl;

import cn.walls1717.wallsmall.common.utils.PageUtils;
import cn.walls1717.wallsmall.common.utils.Query;
import cn.walls1717.wallsmall.order.dao.OrderItemDao;
import cn.walls1717.wallsmall.order.entity.OrderEntity;
import cn.walls1717.wallsmall.order.entity.OrderItemEntity;
import cn.walls1717.wallsmall.order.entity.OrderReturnReasonEntity;
import cn.walls1717.wallsmall.order.service.OrderItemService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@Slf4j
@Service("orderItemService")
@RabbitListener(queues = "hello-java-queue")
public class OrderItemServiceImpl extends ServiceImpl<OrderItemDao, OrderItemEntity> implements OrderItemService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OrderItemEntity> page = this.page(
                new Query<OrderItemEntity>().getPage(params),
                new QueryWrapper<OrderItemEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 根据orderSn查询所有订单项
     *
     * @param orderSn orderSn
     * @return 订单项集合
     */
    @Override
    public List<OrderItemEntity> listByOrderSn(String orderSn) {
        return lambdaQuery().eq(OrderItemEntity::getOrderSn, orderSn).list();
    }

    /*@RabbitListener(queues = "hello-java-queue")
    public void receive(Message msg, OrderReturnReasonEntity context, Channel channel) {
        channel.getConnection();
        byte[] body = msg.getBody();
        log.info("body -> {}", body);
        MessageProperties messageProperties = msg.getMessageProperties();
        log.info("msgProperties -> {}", messageProperties);
        log.info("接收到的消息 -> {}", msg);
        log.info("接收到的内容 -> {}", context);
    }*/

    @RabbitHandler
    public void receive(Message message, OrderReturnReasonEntity reason, Channel channel) {
        log.info("接收到的消息 -> {}", reason);
        // channel内按顺序自增
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        log.info("deliveryTag -> {}", deliveryTag);
        try {
            if (deliveryTag % 2 == 0) {
                channel.basicAck(deliveryTag, false);
                log.info("签收 -> {}", deliveryTag);
            } else {
                channel.basicNack(deliveryTag, false, false);
                log.info("退货 -> {}", deliveryTag);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RabbitHandler
    public void receive2(OrderEntity order) {
        log.info("接收到的消息 -> {}", order);
    }

}